################################################################################
# Judicial Ideology as Text -- Figure A4: Dynamic Ideal Points (Longitudinal)
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source


################################################################################
# Figure A4: Dynamic Ideal Points (Full Weight)
################################################################################

{


  combined_dynamic <- data.frame()

  for (weight in 1:length(wordshoals_dynamic)){

    temp_dynamic <- wordshoals_dynamic[[weight]]

    for (term in 1:length(temp_dynamic)){

      combined_dynamic <- bind_rows(combined_dynamic, temp_dynamic[[term]]$summary_output %>% mutate(term = names(temp_dynamic[term]), weight = names(wordshoals_dynamic[weight])))

    }


  }


  Figure_A4 <- combined_dynamic %>%
    filter(weight == 'Full') %>%
    mutate(
      lower_ci = theta - 1.96 * se,
      upper_ci = theta + 1.96 * se,
      term = term,
      author = ifelse(author == 'Connor', "O'Connor", author),
      ideology = ifelse(author %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    ggplot(aes(x = as.numeric(term), y = theta)) +
    geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci, colour = ideology), width = 0.2, linewidth = 1) +
    geom_point(aes(colour = ideology), size = 2) +
    geom_hline(yintercept = 0, linetype = 2, alpha = 2/3) +
    labs(y = 'Theta\n',
         x = '\nTerm') +
    theme_minimal() +
    facet_wrap(~author) +
    scale_colour_manual(values = c('deepskyblue3', 'red2')) +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 14, colour = 'black'),
          axis.title = element_text(size = 16, colour = 'black'),
          strip.background = element_rect(size = 1, colour = 'black', fill = 'grey'),
          strip.text = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.title = element_blank(),
          legend.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.text = element_text(size = 14, colour = 'black'))

  rm(combined_dynamic, term, weight, temp_dynamic)

  ggsave(Figure_A4,
         file = 'figures/Figure_A4.png',
         height = 10, width = 12, units = 'in', bg = "white")


  ggsave('figures_tiff/Figure_A4.tiff',
         plot = Figure_A4,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

} # Figure A4: Dynamic Ideal Points (Full Weight)
